<template>
	<div class="tikuBg">
		
		<div class="banner">
			<image src="https://www.baixuemianshi.com/zhuanti/images/swiper_xcx2.jpg" mode="widthFix"></image>
		</div>

		<div class="zutiBox">
			<div class="item" v-for="(item,index) in zutiReverse" :key="index">
				<div  @click="getTimuDetail(item)">
					<h3>{{ item.title }}</h3>
				</div>
			</div>
		</div>
		<canvas canvas-id="waterMark" class="waterMark" style="width: 100vw; height: 100vh;"></canvas>
		<image :src="waterMarkUrl" class="waterMark-bg" style="width: 100vw; height: 100vh;"></image>
	</div>
</template>

<script>
	import {
		mapActions
	} from 'vuex';
	const db = wx.cloud.database()
	export default {
		data() {
			return {
				waterMarkUrl: "",
				windowWidth: 100,
				windowHeight: 100,
				userinfo: {},
				nolimit: false,
				limitStatus:false,
				moshiList: [
					{name: "组题模式",spell: "zuti"},
					{name: "作业模式",spell: "homework"},
				],
				selectMoshi:{name: "组题模式",spell: "zuti"},
				banjiDetail:"",

			}
		},
		onShareAppMessage(res) {
			if (res.from === 'button') { // 来自页面内分享按钮
				return {
					title: `题库-学员端`, //分享标题
					path: '/pages/nbhd/tiku/student', //分享页面路径
					imageUrl: '', //分享图标
					desc: '雪恒教育-白雪面试', //自定义分享描述 
				}
			} else {
				return {
					title: `题库-学员端`, //分享标题
					path: '/pages/nbhd/tiku/student', //分享页面路径
					imageUrl: '', //分享图标
					desc: '雪恒教育-白雪面试', //自定义分享描述 
				}
			}
		},
		onLoad(options) {
			
		},
		onReady(e) {
			
		},
		onShow() {
			if(wx.setVisualEffectOnCapture){
			    wx.setVisualEffectOnCapture({
			        visualEffect: 'hidden',
			        success:(res) => {
			            console.log('success回调',res)
			        },
			    })
			}
		},
		onHide() {
			if(wx.setVisualEffectOnCapture){
			    wx.setVisualEffectOnCapture({
			        visualEffect: 'none',
			        success:(res) => {
			            console.log('success回调',res)
			        },
			    })
			}
		},
		computed: {
			userphone() {
				return this.$store.getters.userphone ? this.$store.getters.userphone : '';
			},
			selectBanji(){
				return this.$store.getters.selectBanji ? this.$store.getters.selectBanji : '';
			},
			zutiReverse(){
				if(this.banjiDetail){
					let arr = this.banjiDetail.homework
					arr = [...arr].reverse()
					return arr
				}else{
					return []
				}
			},
			homeworkReverse(){
				if(this.banjiDetail){
					let arr = this.banjiDetail.homework
					arr = [...arr].reverse()
					return arr
				}else{
					return []
				}
			},
		},
		created() {
			let that = this;
			
		},
		mounted() {
			let that = this 
			that.getTimuInfo()
			wx.hideLoading()
			
			if(this.userphone){
				this.drawWaterMark(this.userphone);
			}else{
				this.drawWaterMark('雪恒教育-白雪面试');
			}
		},
		watch: {},
		methods: {
			...mapActions(['LogSetPhone','LogSetBanji']),
			drawWaterMark(text) {
				this.getDeviceInfo().then(response => {
					const w = response.windowWidth;
					const h = response.windowHeight;
					this.windowWidth = w;
					this.windowHeight = h;
					//创建canvas绘制上下文
					const canvas = uni.createCanvasContext("waterMark", this);
					canvas.clearRect(0, 0, w, h)
					//开始创建一个路径，需要调用fill或者stroke才会使用路径进行填充或描边。
					canvas.beginPath();
					canvas.setFontSize(18);
					canvas.setFillStyle('rgba(0, 0, 0, 0.1)');
					canvas.rotate(-30 * Math.PI / 180)
					if (text) {
						text = this.trim(text)
					}
					const maxWidth = text.length * 18
					for (let x = 0; x < 10; x++) {
						for (let y = 0; y < 20; y++) {
							//在画布上绘制被填充的文本。
							canvas.fillText(text, (maxWidth + 40) * x - 400, 90 * y + 20);
						}
					}
					//将之前在绘图上下文中的描述（路径、变形、样式）画到 canvas 中。
					canvas.draw(false, () => {
						//把当前画布指定区域的内容导出生成指定大小的图片
						uni.canvasToTempFilePath({
							canvasId: "waterMark",
							complete: (res) => {
								this.waterMarkUrl = res.tempFilePath
							}
						}, this)
					})
				});
			},
			trim(str) {
				let trimStr = '';
				const reg = /[\t\r\f\n\s]*/g;
				if (typeof str === 'string') {
					trimStr = str.replace(reg, '');
				}
				return trimStr
			},
			/**
			 * 获取当前设备的信息
			 * **/
			getDeviceInfo() {
				return new Promise((resolve, reject) => {
					uni.getSystemInfo({
						success: (sysInfo) => {
							resolve(sysInfo)
						}
					})
				})
			},
			changeMoshi(e){
				this.selectMoshi = {
					name: e.name,
					spell: e.spell
				}
			},
			getTimuInfo(){
				let that = this
				db.collection('nbhd_tiku_banji').where({
					banji: that.selectBanji
				}).get({
					success: function(res) {
						if(res.data.length > 0){
							that.banjiDetail = res.data[0]
						}
					}
				})
			},
			getTimuDetail(info){
				uni.navigateTo({
					url:'./homeworkDetail_s?title=' + info.title
				})
			}
		},
		components: {
			
		}
	};
</script>

<style scoped>
	@import url(./index.css);
	.tabsBox{background-color: #fff;}
	.zutiBox{}
	.zutiBox .item{position: relative; width: 650rpx; margin: 30rpx auto 0; background-color: #fff; padding: 25rpx; border-radius: 15rpx; box-shadow: 0 0 15rpx rgba(0,0,0,.2);}
	.zutiBox .item h3{font-size: 30rpx; font-weight: bold;}
	.zutiBox .item .type{display: flex; margin: 20rpx 0;}
	.zutiBox .item .info{font-size: 28rpx; }
	
	.popContent h3{font-size: 34rpx; font-weight: bold; text-align: center; margin-bottom: 15rpx;}
	.deleteItem{position: absolute; right: 25rpx; bottom: 25rpx; width: 100rpx; z-index: 10;}
	
	.btnBox{display: flex; justify-content: space-between;}
	.btnBox .btn{width: 48%;}
</style>